<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>FabGL: VGA/PCEmulator/PCEmulator.ino</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">FabGL
   </div>
   <div id="projectbrief">ESP32 Display Controller and Graphics Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_v_g_a_2_p_c_emulator_2_p_c_emulator_8ino-example.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">VGA/PCEmulator/PCEmulator.ino</div>  </div>
</div><!--header-->
<div class="contents">
<p>A i8086 based IBM PC emulator (runs FreeDOS, MS-DOS, CPM86, Linux-ELK, Windows 3.0)</p>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - &lt;http://www.fabgl.com&gt;</span></div><div class="line"><span class="comment">  Copyright (c) 2019-2021 Fabrizio Di Vittorio.</span></div><div class="line"><span class="comment">  All rights reserved.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">* Please contact fdivitto2013@gmail.com if you need a commercial license.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">* This library and related software is available under GPL v3.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is free software: you can redistribute it and/or modify</span></div><div class="line"><span class="comment">  it under the terms of the GNU General Public License as published by</span></div><div class="line"><span class="comment">  the Free Software Foundation, either version 3 of the License, or</span></div><div class="line"><span class="comment">  (at your option) any later version.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  FabGL is distributed in the hope that it will be useful,</span></div><div class="line"><span class="comment">  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div><div class="line"><span class="comment">  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div><div class="line"><span class="comment">  GNU General Public License for more details.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  You should have received a copy of the GNU General Public License</span></div><div class="line"><span class="comment">  along with FabGL.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"></div><div class="line"> <span class="comment">/* Instructions:</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">    - to run this application you need an ESP32 with PSRAM installed and an SD-CARD slot (ie TTGO VGA32 v1.4)</span></div><div class="line"><span class="comment">    - open this with Arduino and make sure PSRAM is DISABLED</span></div><div class="line"><span class="comment">    - partition scheme must be: Huge App</span></div><div class="line"><span class="comment">    - compile and upload the sketch</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#pragma message &quot;This sketch requires Tools-&gt;Partition Scheme = Huge APP&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;esp32-hal-psram.h&quot;</span></div><div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div><div class="line"><span class="preprocessor">#include &quot;esp_spiram.h&quot;</span></div><div class="line">}</div><div class="line"><span class="preprocessor">#include &quot;esp_sntp.h&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;Preferences.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;WiFi.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;HTTPClient.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="fabgl_8h.html">fabgl.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;machine.h&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">using</span> fabgl::StringList;</div><div class="line"><span class="keyword">using</span> fabgl::imin;</div><div class="line"><span class="keyword">using</span> fabgl::imax;</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">struct </span>DiskConf {</div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * desc;</div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * diskA_URL;</div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * diskC_URL;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">const</span> DiskConf diskConfs[] = {</div><div class="line">  { <span class="stringliteral">&quot;FreeDOS (A:)&quot;</span>,                               <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_freedos.img&quot;</span>,  <span class="keyword">nullptr</span> },</div><div class="line">  { <span class="stringliteral">&quot;FreeDOS (A:) + DOS Programming Tools (C:)&quot;</span>,  <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_freedos.img&quot;</span>,  <span class="stringliteral">&quot;http://www.fabglib.org/downloads/C_dosdev.img&quot;</span> },</div><div class="line">  { <span class="stringliteral">&quot;FreeDOS (A:) + Windows 3.0 Hercules (C:)&quot;</span>,   <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_freedos.img&quot;</span>,  <span class="stringliteral">&quot;http://www.fabglib.org/downloads/C_winherc.img&quot;</span> },</div><div class="line">  { <span class="stringliteral">&quot;FreeDOS (A:) + DOS Programs and Games (C:)&quot;</span>, <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_freedos.img&quot;</span>,  <span class="stringliteral">&quot;http://www.fabglib.org/downloads/C_dosprog.img&quot;</span> },</div><div class="line">  { <span class="stringliteral">&quot;MS-DOS 3.31 (A:)&quot;</span>,                           <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_MSDOS331.img&quot;</span>, <span class="keyword">nullptr</span> },</div><div class="line">  { <span class="stringliteral">&quot;Linux ELKS 0.4.0&quot;</span>,                           <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_ELK040.img&quot;</span>,   <span class="keyword">nullptr</span> },</div><div class="line">  { <span class="stringliteral">&quot;CP/M 86 + Turbo Pascal 3&quot;</span>,                   <span class="stringliteral">&quot;http://www.fabglib.org/downloads/A_CPM86.img&quot;</span>,    <span class="keyword">nullptr</span> },</div><div class="line">};</div><div class="line"></div><div class="line"></div><div class="line">Preferences preferences;</div><div class="line">InputBox    ibox;</div><div class="line">Machine     * machine;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// try to connected using saved parameters</span></div><div class="line"><span class="keywordtype">bool</span> tryToConnect()</div><div class="line">{</div><div class="line">  <span class="keywordtype">bool</span> connected = WiFi.status() == WL_CONNECTED;</div><div class="line">  <span class="keywordflow">if</span> (!connected) {</div><div class="line">    <span class="keywordtype">char</span> SSID[32] = <span class="stringliteral">&quot;&quot;</span>;</div><div class="line">    <span class="keywordtype">char</span> psw[32]  = <span class="stringliteral">&quot;&quot;</span>;</div><div class="line">    <span class="keywordflow">if</span> (preferences.getString(<span class="stringliteral">&quot;SSID&quot;</span>, SSID, <span class="keyword">sizeof</span>(SSID)) &amp;&amp; preferences.getString(<span class="stringliteral">&quot;WiFiPsw&quot;</span>, psw, <span class="keyword">sizeof</span>(psw))) {</div><div class="line">      ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Abort&quot;</span>, <span class="keyword">true</span>, 200, [&amp;](fabgl::ProgressApp * app) {</div><div class="line">        WiFi.begin(SSID, psw);</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 32 &amp;&amp; WiFi.status() != WL_CONNECTED; ++i) {</div><div class="line">          <span class="keywordflow">if</span> (!app-&gt;update(i * 100 / 32, <span class="stringliteral">&quot;Connecting to %s...&quot;</span>, SSID))</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">          delay(500);</div><div class="line">          <span class="keywordflow">if</span> (i == 16)</div><div class="line">            WiFi.reconnect();</div><div class="line">        }</div><div class="line">        connected = (WiFi.status() == WL_CONNECTED);</div><div class="line">      });</div><div class="line">      <span class="comment">// show to user the connection state</span></div><div class="line">      <span class="keywordflow">if</span> (!connected) {</div><div class="line">        WiFi.disconnect();</div><div class="line">        ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;WiFi Connection failed!&quot;</span>);</div><div class="line">      }</div><div class="line">    }</div><div class="line">  }</div><div class="line">  <span class="keywordflow">return</span> connected;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> checkWiFi()</div><div class="line">{</div><div class="line">  <span class="keywordtype">bool</span> connected = tryToConnect();</div><div class="line">  <span class="keywordflow">if</span> (!connected) {</div><div class="line"></div><div class="line">    <span class="comment">// configure WiFi?</span></div><div class="line">    <span class="keywordflow">if</span> (ibox.message(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Configure WiFi?&quot;</span>, <span class="stringliteral">&quot;No&quot;</span>, <span class="stringliteral">&quot;Yes&quot;</span>) == InputResult::Enter) {</div><div class="line"></div><div class="line">      <span class="comment">// repeat until connected or until user cancels</span></div><div class="line">      <span class="keywordflow">do</span> {</div><div class="line"></div><div class="line">        <span class="comment">// yes, scan for networks showing a progress dialog box</span></div><div class="line">        <span class="keywordtype">int</span> networksCount = 0;</div><div class="line">        ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">false</span>, 200, [&amp;](fabgl::ProgressApp * app) {</div><div class="line">          app-&gt;update(0, <span class="stringliteral">&quot;Scanning WiFi networks...&quot;</span>);</div><div class="line">          networksCount = WiFi.scanNetworks();</div><div class="line">        });</div><div class="line"></div><div class="line">        <span class="comment">// are there available WiFi?</span></div><div class="line">        <span class="keywordflow">if</span> (networksCount &gt; 0) {</div><div class="line"></div><div class="line">          <span class="comment">// yes, show a selectable list</span></div><div class="line">          StringList list;</div><div class="line">          <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; networksCount; ++i)</div><div class="line">            list.appendFmt(<span class="stringliteral">&quot;%s (%d dBm)&quot;</span>, WiFi.SSID(i).c_str(), WiFi.RSSI(i));</div><div class="line">          <span class="keywordtype">int</span> s = ibox.menu(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Please select a WiFi network&quot;</span>, &amp;list);</div><div class="line"></div><div class="line">          <span class="comment">// user selected something?</span></div><div class="line">          <span class="keywordflow">if</span> (s &gt; -1) {</div><div class="line">            <span class="comment">// yes, ask for WiFi password</span></div><div class="line">            <span class="keywordtype">char</span> psw[32] = <span class="stringliteral">&quot;&quot;</span>;</div><div class="line">            <span class="keywordflow">if</span> (ibox.textInput(<span class="stringliteral">&quot;WiFi Configuration&quot;</span>, <span class="stringliteral">&quot;Insert WiFi password&quot;</span>, psw, 31, <span class="stringliteral">&quot;Cancel&quot;</span>, <span class="stringliteral">&quot;OK&quot;</span>, <span class="keyword">true</span>) == InputResult::Enter) {</div><div class="line">              <span class="comment">// user pressed OK, connect to WiFi...</span></div><div class="line">              preferences.putString(<span class="stringliteral">&quot;SSID&quot;</span>, WiFi.SSID(s).c_str());</div><div class="line">              preferences.putString(<span class="stringliteral">&quot;WiFiPsw&quot;</span>, psw);</div><div class="line">              connected = tryToConnect();</div><div class="line">              <span class="comment">// show to user the connection state</span></div><div class="line">              <span class="keywordflow">if</span> (connected)</div><div class="line">                ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Connection succeeded!&quot;</span>);</div><div class="line">            } <span class="keywordflow">else</span></div><div class="line">              <span class="keywordflow">break</span>;</div><div class="line">          } <span class="keywordflow">else</span></div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        } <span class="keywordflow">else</span> {</div><div class="line">          <span class="comment">// there is no WiFi</span></div><div class="line">          ibox.message(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;No WiFi network found!&quot;</span>);</div><div class="line">          <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">        WiFi.scanDelete();</div><div class="line"></div><div class="line">      } <span class="keywordflow">while</span> (!connected);</div><div class="line"></div><div class="line">    }</div><div class="line"></div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> connected;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> updateDateTime()</div><div class="line">{</div><div class="line">  <span class="comment">// Set timezone</span></div><div class="line">  setenv(<span class="stringliteral">&quot;TZ&quot;</span>, <span class="stringliteral">&quot;CET-1CEST,M3.5.0,M10.5.0/3&quot;</span>, 1);</div><div class="line">  tzset();</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (checkWiFi()) {</div><div class="line"></div><div class="line">    <span class="comment">// we need time right now</span></div><div class="line">    ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">true</span>, 200, [&amp;](fabgl::ProgressApp * app) {</div><div class="line">      sntp_setoperatingmode(SNTP_OPMODE_POLL);</div><div class="line">      sntp_setservername(0, (<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;pool.ntp.org&quot;</span>);</div><div class="line">      sntp_init();</div><div class="line">      <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 12 &amp;&amp; sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED; ++i) {</div><div class="line">        app-&gt;update(i * 100 / 12, <span class="stringliteral">&quot;Getting date-time from SNTP...&quot;</span>);</div><div class="line">        delay(500);</div><div class="line">      }</div><div class="line">      sntp_stop();</div><div class="line">    });</div><div class="line"></div><div class="line">  } <span class="keywordflow">else</span> {</div><div class="line"></div><div class="line">    <span class="comment">// set default time</span></div><div class="line">    <span class="comment">//printf(&quot;Setting default date/time\n&quot;);</span></div><div class="line">    <span class="keyword">auto</span> tm = (<span class="keyword">struct </span>tm){ .tm_sec  = 0, .tm_min  = 0, .tm_hour = 8, .tm_mday = 14, .tm_mon  = 7, .tm_year = 84 };</div><div class="line">    <span class="keyword">auto</span> now = (timeval){ .tv_sec = mktime(&amp;tm) };</div><div class="line">    settimeofday(&amp;now, <span class="keyword">nullptr</span>);</div><div class="line"></div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// download specified filename from URL</span></div><div class="line"><span class="keywordtype">bool</span> downloadURL(<span class="keywordtype">char</span> <span class="keyword">const</span> * URL, FILE * file)</div><div class="line">{</div><div class="line">  <span class="keywordtype">bool</span> success = <span class="keyword">false</span>;</div><div class="line"></div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * filename = strrchr(URL, <span class="charliteral">&#39;/&#39;</span>) + 1;</div><div class="line"></div><div class="line">  ibox.progressBox(<span class="stringliteral">&quot;&quot;</span>, <span class="stringliteral">&quot;Abort&quot;</span>, <span class="keyword">true</span>, 380, [&amp;](fabgl::ProgressApp * app) {</div><div class="line">    app-&gt;update(0, <span class="stringliteral">&quot;Preparing to download %s&quot;</span>, filename);</div><div class="line">    HTTPClient http;</div><div class="line">    http.begin(URL);</div><div class="line">    <span class="keywordtype">int</span> httpCode = http.GET();</div><div class="line">    <span class="keywordflow">if</span> (httpCode == HTTP_CODE_OK) {</div><div class="line">      <span class="keywordflow">if</span> (file) {</div><div class="line">        <span class="keywordtype">int</span> tlen = http.getSize();</div><div class="line">        <span class="keywordtype">int</span> tlenK = tlen / 1024;</div><div class="line">        <span class="keywordtype">int</span> len = tlen;</div><div class="line">        constexpr <span class="keywordtype">int</span> BUFSIZE = 6000;</div><div class="line">        uint8_t * buf = (uint8_t*) malloc(BUFSIZE);</div><div class="line">        WiFiClient * stream = http.getStreamPtr();</div><div class="line">        <span class="keywordtype">int</span> dsize = 0;</div><div class="line">        <span class="keywordflow">while</span> (http.connected() &amp;&amp; (len &gt; 0 || len == -1)) {</div><div class="line">          <span class="keywordtype">size_t</span> size = stream-&gt;available();</div><div class="line">          <span class="keywordflow">if</span> (size) {</div><div class="line">            <span class="keywordtype">int</span> c = stream-&gt;readBytes(buf, imin(BUFSIZE, size));</div><div class="line">            fwrite(buf, c, 1, file);</div><div class="line">            dsize += c;</div><div class="line">            <span class="keywordflow">if</span> (len &gt; 0)</div><div class="line">              len -= c;</div><div class="line">            <span class="keywordflow">if</span> (!app-&gt;update(dsize / 1024 * 100 / tlenK, <span class="stringliteral">&quot;Downloading %s (%.2f / %d MB)&quot;</span>, filename, (<span class="keywordtype">double</span>)dsize / 1048576.0, tlenK / 1024))</div><div class="line">              <span class="keywordflow">break</span>;</div><div class="line">          }</div><div class="line">        }</div><div class="line">        free(buf);</div><div class="line">        success = (len == 0 || (len == -1 &amp;&amp; dsize &gt; 0));</div><div class="line">      }</div><div class="line">    }</div><div class="line">  });</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> success;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> setup()</div><div class="line">{</div><div class="line">  Serial.begin(115200); delay(500); printf(<span class="stringliteral">&quot;\n\n\nReset\n\n&quot;</span>);<span class="comment">// DEBUG ONLY</span></div><div class="line"></div><div class="line">  disableCore0WDT();</div><div class="line">  delay(100); <span class="comment">// experienced crashes without this delay!</span></div><div class="line">  disableCore1WDT();</div><div class="line"></div><div class="line">  preferences.begin(<span class="stringliteral">&quot;PCEmulator&quot;</span>, <span class="keyword">false</span>);</div><div class="line"></div><div class="line">  <span class="comment">// uncomment to clear preferences</span></div><div class="line">  <span class="comment">//preferences.clear();</span></div><div class="line"></div><div class="line">  ibox.begin(<a name="a0"></a><a class="code" href="fabglconf_8h_a86b59f35df218823a6165d836cfc22c0.html#a86b59f35df218823a6165d836cfc22c0">VGA_640x400_70Hz</a>, 400, 300); <span class="comment">// VGA_640x400_60Hz as alternative?</span></div><div class="line">  ibox.setBackgroundColor(RGB888(0, 0, 0));</div><div class="line"></div><div class="line">  <span class="comment">// we need PSRAM for this app, but we will handle it manually, so please DO NOT enable PSRAM on your development env</span></div><div class="line"><span class="preprocessor">  #ifdef BOARD_HAS_PSRAM</span></div><div class="line">  ibox.message(<span class="stringliteral">&quot;Warning!&quot;</span>, <span class="stringliteral">&quot;Please disable PSRAM to improve performance!&quot;</span>);</div><div class="line"><span class="preprocessor">  #endif</span></div><div class="line"></div><div class="line">  <span class="comment">// note: we use just 2MB of PSRAM so the infamous PSRAM bug should not happen. But to avoid gcc compiler hack (-mfix-esp32-psram-cache-issue)</span></div><div class="line">  <span class="comment">// we enable PSRAM at runtime, otherwise the hack slows down CPU too much (PSRAM_HACK is no more required).</span></div><div class="line">  <span class="keywordflow">if</span> (esp_spiram_init() != ESP_OK)</div><div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;This app requires a board with PSRAM!&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">nullptr</span>);</div><div class="line"></div><div class="line"><span class="preprocessor">  #ifndef BOARD_HAS_PSRAM</span></div><div class="line">  esp_spiram_init_cache();</div><div class="line"><span class="preprocessor">  #endif</span></div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!FileBrowser::mountSDCard(<span class="keyword">false</span>, <span class="stringliteral">&quot;/SD&quot;</span>, 8))</div><div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;This app requires a SD-CARD!&quot;</span>, <span class="keyword">nullptr</span>, <span class="keyword">nullptr</span>);</div><div class="line"></div><div class="line">  <span class="comment">// uncomment to format SD!</span></div><div class="line">  <span class="comment">//FileBrowser::format(fabgl::DriveType::SDCard, 0);</span></div><div class="line"></div><div class="line">  updateDateTime();</div><div class="line"></div><div class="line">  <span class="comment">// show a list of disks configurations</span></div><div class="line">  StringList dconfs;</div><div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <span class="keyword">sizeof</span>(diskConfs) / <span class="keyword">sizeof</span>(DiskConf); ++i)</div><div class="line">    dconfs.append(diskConfs[i].desc);</div><div class="line">  dconfs.select(preferences.getInt(<span class="stringliteral">&quot;dconf&quot;</span>, 0), <span class="keyword">true</span>);</div><div class="line">  ibox.select(<span class="stringliteral">&quot;Disks Configurations&quot;</span>, <span class="stringliteral">&quot;Please select a disk configuration&quot;</span>, &amp;dconfs, <span class="keyword">nullptr</span>, <span class="stringliteral">&quot;OK&quot;</span>, 8);</div><div class="line">  <span class="keywordtype">int</span> idx = imax(dconfs.getFirstSelected(), 0);</div><div class="line">  preferences.putInt(<span class="stringliteral">&quot;dconf&quot;</span>, idx);</div><div class="line"></div><div class="line">  <span class="comment">// load selected configuration</span></div><div class="line">  FileBrowser fb;</div><div class="line">  fb.setDirectory(<span class="stringliteral">&quot;/SD&quot;</span>);</div><div class="line">  <span class="keywordtype">bool</span> disksExist = <span class="keyword">true</span>;</div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * filenameDiskA = <span class="keyword">nullptr</span>;</div><div class="line">  <span class="keywordtype">char</span> <span class="keyword">const</span> * filenameDiskC = <span class="keyword">nullptr</span>;</div><div class="line">  <span class="keywordflow">if</span> (diskConfs[idx].diskA_URL) {</div><div class="line">    filenameDiskA = strrchr(diskConfs[idx].diskA_URL, <span class="charliteral">&#39;/&#39;</span>) + 1;</div><div class="line">    disksExist = fb.exists(filenameDiskA, <span class="keyword">false</span>);</div><div class="line">    <span class="keywordflow">if</span> (!disksExist) {</div><div class="line">      <span class="comment">// disk A doesn&#39;t exist, download</span></div><div class="line">      <span class="keyword">auto</span> file = fb.openFile(filenameDiskA, <span class="stringliteral">&quot;wb&quot;</span>);</div><div class="line">      disksExist = downloadURL(diskConfs[idx].diskA_URL, file);</div><div class="line">      fclose(file);</div><div class="line">      <span class="keywordflow">if</span> (!disksExist)</div><div class="line">        fb.remove(filenameDiskA);</div><div class="line">    }</div><div class="line">  }</div><div class="line">  <span class="keywordflow">if</span> (diskConfs[idx].diskC_URL) {</div><div class="line">    filenameDiskC = strrchr(diskConfs[idx].diskC_URL, <span class="charliteral">&#39;/&#39;</span>) + 1;</div><div class="line">    disksExist = disksExist &amp;&amp; fb.exists(filenameDiskC, <span class="keyword">false</span>);</div><div class="line">    <span class="keywordflow">if</span> (!disksExist) {</div><div class="line">      <span class="comment">// disk C doesn&#39;t exist, download</span></div><div class="line">      <span class="keyword">auto</span> file = fb.openFile(filenameDiskC, <span class="stringliteral">&quot;wb&quot;</span>);</div><div class="line">      disksExist = downloadURL(diskConfs[idx].diskC_URL, file);</div><div class="line">      fclose(file);</div><div class="line">      <span class="keywordflow">if</span> (!disksExist)</div><div class="line">        fb.remove(filenameDiskC);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (!disksExist) {</div><div class="line">    <span class="comment">// unable to get disks</span></div><div class="line">    ibox.message(<span class="stringliteral">&quot;Error!&quot;</span>, <span class="stringliteral">&quot;Unable to get system disks!&quot;</span>);</div><div class="line">    esp_restart();</div><div class="line">  }</div><div class="line"></div><div class="line">  ibox.end();</div><div class="line"></div><div class="line"></div><div class="line">  machine = <span class="keyword">new</span> Machine;</div><div class="line">  machine-&gt;setDriveA(filenameDiskA);</div><div class="line">  machine-&gt;setDriveC(filenameDiskC);</div><div class="line">  machine-&gt;run();</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="preprocessor">#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK</span></div><div class="line"><span class="keyword">namespace </span><a class="code" href="namespacefabgl.html">fabgl</a> {</div><div class="line">  <span class="keyword">extern</span> <span class="keyword">volatile</span> uint64_t s_vgapalctrlcycles;</div><div class="line">}</div><div class="line"><span class="keyword">using</span> fabgl::s_vgapalctrlcycles;</div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> loop()</div><div class="line">{</div><div class="line"></div><div class="line"><span class="preprocessor">#if FABGLIB_VGAXCONTROLLER_PERFORMANCE_CHECK</span></div><div class="line">  <span class="keyword">static</span> uint32_t tcpu = 0, s1 = 0, count = 0;</div><div class="line">  tcpu = machine-&gt;ticksCounter();</div><div class="line">  s_vgapalctrlcycles = 0;</div><div class="line">  s1 = fabgl::getCycleCount();</div><div class="line">  delay(1000);</div><div class="line">  printf(<span class="stringliteral">&quot;%d\tCPU: %d&quot;</span>, count, machine-&gt;ticksCounter() - tcpu);</div><div class="line">  printf(<span class="stringliteral">&quot;   Graph: %lld / %d   (%d%%)\n&quot;</span>, s_vgapalctrlcycles, fabgl::getCycleCount() - s1, (<span class="keywordtype">int</span>)((<span class="keywordtype">double</span>)s_vgapalctrlcycles/240000000*100));</div><div class="line">  ++count;</div><div class="line"><span class="preprocessor">#else</span></div><div class="line"></div><div class="line">  vTaskDelete(NULL);</div><div class="line"></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line">}</div><div class="line"></div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Mon Aug 2 2021 07:08:02 for FabGL by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
